use nacos_cluster::manager::ClusterManager;
use nacos_cluster::node::ClusterNodeInfo;
use std::sync::Arc;
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() -> nacos_core::Result<()> {
    println!("🏛️ 集群选举故障测试");
    println!("========================");
    
    // 创建集群管理器
    let config = nacos_cluster::manager::ClusterConfig {
        cluster_name: "test-cluster".to_string(),
        heartbeat_timeout: 3,
        election_timeout: 1000,
        current_node_id: "test-node".to_string(),
        initial_nodes: vec!["node1:8848".to_string(), "node2:8849".to_string(), "node3:8850".to_string()],
    };
    
    let manager = Arc::new(ClusterManager::new(config));
    let node = ClusterNodeInfo::new("127.0.0.1", 8848, 1);
    
    println!("📊 初始化节点: {}:8848", node.id);
    
    // 初始化集群
    manager.initialize(node).await?;
    
    // 模拟集群操作
    println!("\n🚀 模拟集群操作...");
    
    // 检查集群状态
    let status = manager.get_cluster_status().await?;
    println!("📈 集群状态:");
    println!("   状态: {:?}", status.state);
    println!("   总节点: {}", status.total_nodes);
    println!("   健康节点: {}", status.healthy_nodes);
    
    // 模拟节点故障检测
    println!("\n⚡ 模拟故障检测...");
    manager.mark_node_failed("node1").await?;
    
    sleep(Duration::from_secs(1)).await;
    
    let new_status = manager.get_cluster_status().await?;
    println!("🔄 故障后状态:");
    println!("   故障节点: {}", new_status.failed_nodes);
    println!("   健康节点: {}", new_status.healthy_nodes);
    
    // 模拟故障恢复
    println!("\n🔄 模拟故障恢复...");
    manager.mark_node_recovered("node1").await?;
    
    sleep(Duration::from_secs(1)).await;
    
    let final_status = manager.get_cluster_status().await?;
    println!("✅ 恢复后状态:");
    println!("   健康节点: {}", final_status.healthy_nodes);
    println!("   故障节点: {}", final_status.failed_nodes);
    
    manager.shutdown().await?;
    
    println!("\n🎯 集群测试完成！");
    
    Ok(())
}